<?php

namespace App\Http\Controllers;

use App\Logic\OtherInfoLogic;
use App\Exceptions\ApiException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OtherInfoController extends Controller
{
    /**
     * 其他信息列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request): JsonResponse
    {
        $request->validate([
            'page'      => 'sometimes|integer|min:1',
            'page_size' => 'sometimes|integer|min:1|max:100',
            'user_id'   => 'sometimes|string',
            'title'     => 'sometimes|string',
            'details'   => 'sometimes|string',
            'state'     => 'sometimes|string',
            'end_time'  => 'sometimes|date',
        ]);
        $params = $request->only(['page', 'page_size', 'user_id', 'title', 'details', 'state', 'end_time']);
        $logic  = new OtherInfoLogic();
        $data   = $logic->list($params);
        return $this->OkJson($data);
    }

    /**
     * 新增其他信息
     * @param Request $request
     * @return JsonResponse
     */
    public function create(Request $request): JsonResponse
    {
        $request->validate([
            'user_id'   => 'required|string',
            'user_name' => 'required|string',
            'title'     => 'required|string',
            'details'   => 'required|string',
            'state'     => 'required|string',
            'remark'    => 'sometimes|string',
            'weight'    => 'sometimes|integer',
            'end_time'  => 'sometimes|date',

        ]);
        $params = $request->only(['user_id', 'user_name', 'title', 'details', 'state', 'remark', 'weight', 'end_time']);
        $logic  = new OtherInfoLogic();
        $data   = $logic->create($params);
        return $this->OkJson($data);
    }

    /**
     * 软删除其他信息
     * @param Request $request
     * @return JsonResponse
     */
    public function delete(Request $request): JsonResponse
    {
        $request->validate([
            'id' => 'required|integer',
        ]);
        $logic = new OtherInfoLogic();
        $logic->delete($request->input('id'));
        return $this->OkJson(['msg' => '删除成功']);
    }
}
